package services

import (
	"errors"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/requests"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/responses"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/transports/http/middlewares/jwtoken"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/captchautil"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/constutil"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/stringutil"
	"go.uber.org/zap"
	"time"
)

// TestExist 字段存在
func (s *DefaultService) TestExist(req *requests.TextExists) (result bool) {
	user, _ := s.Repository.TestExist(req)
	if user.ID > 0 {
		result = true
	}
	return
}

// Register 注册
func (s *DefaultService) Register(req *requests.Register) (err error) {
	//判断验证码是否正确
	if !captchautil.VerifyString(req.IdKeyC, req.Captcha) {
		err = errors.New("验证码输入错误")
		return
	}
	user, _ := s.Repository.TestExist(&requests.TextExists{
		Key:   constutil.EmailField,
		Value: req.Email,
	})
	if user.ID > 0 {
		err = errors.New("邮箱已存在")
		return
	}
	user, _ = s.Repository.TestExist(&requests.TextExists{
		Key:   constutil.UsernameField,
		Value: req.Username,
	})
	if user.ID > 0 {
		err = errors.New("用户昵称已存在")
		return
	}
	err = s.Repository.Register(req)
	return
}

// Login 登陆
func (s *DefaultService) Login(req *requests.Login) (userLogin *responses.UserLogin, err error) {
	//判断验证码是否正确
	if !captchautil.VerifyString(req.IdKeyC, req.Captcha) {
		err = errors.New("验证码输入错误")
		return
	}
	user, err := s.Repository.TestExist(&requests.TextExists{
		Key:   constutil.EmailField,
		Value: req.Email,
	})
	if err != nil || user.ID == 0 {
		err = errors.New("登陆邮箱输入错误")
		return
	}
	if user.Password != stringutil.CryptPassword(req.Password, user.Salt) {
		err = errors.New("密码输入错误")
		return
	}
	token, err := jwtoken.GenToken(user.Username, user.Avatar, user.ID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		err = errors.New("生成token失效")
		return
	}
	userLogin = &responses.UserLogin{
		ID:       user.ID,
		GroupID:  user.GroupID,
		Username: user.Username,
		Email:    user.Email,
		Mobile:   user.Mobile,
		Avatar:   user.Avatar,
		Level:    user.Level,
		Gender:   user.Gender,
		Bio:      user.Bio,
		Score:    user.Score,
		Token:    token,
	}
	return
}

// User 用户信息
func (s *DefaultService) GetUser(userID int) (userLogin *responses.UserLogin, err error) {
	user, err := s.Repository.GetUser(userID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		err = errors.New("用户不存在")
		return
	}
	token, err := jwtoken.GenToken(user.Username, user.Avatar, user.ID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		err = errors.New("生成token失效")
		return
	}
	userLogin = &responses.UserLogin{
		ID:       user.ID,
		GroupID:  user.GroupID,
		Username: user.Username,
		Email:    user.Email,
		Mobile:   user.Mobile,
		Avatar:   user.Avatar,
		Level:    user.Level,
		Gender:   user.Gender,
		Bio:      user.Bio,
		Score:    user.Score,
		Token:    token,
	}
	return
}

// User 用户信息
func (s *DefaultService) Avatar(avatar string, userID int) (userLogin *responses.UserLogin, err error) {
	user, err := s.Repository.GetUser(userID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		return
	}
	err = s.Repository.Avatar(userID, avatar)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		return
	}
	token, err := jwtoken.GenToken(user.Username, user.Avatar, user.ID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		err = errors.New("生成token失效")
		return
	}
	userLogin = &responses.UserLogin{
		ID:       user.ID,
		GroupID:  user.GroupID,
		Username: user.Username,
		Email:    user.Email,
		Mobile:   user.Mobile,
		Avatar:   avatar,
		Level:    user.Level,
		Gender:   user.Gender,
		Bio:      user.Bio,
		Score:    user.Score,
		Token:    token,
	}
	return
}

// Pwd 修改密码
func (s *DefaultService) Pwd(req *requests.Pwd) (err error) {
	user, err := s.Repository.GetUser(req.UserID)
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		return
	}
	if user.Password != stringutil.CryptPassword(req.OldPwd, user.Salt) {
		err = errors.New("原密码输入错误")
		return
	}
	return s.Repository.Pwd(req, user.Salt)
}

// ResetPwd 修改密码
func (s *DefaultService) ResetPwd(req *requests.ResetPwd) (err error) {
	user, err := s.Repository.TestExist(&requests.TextExists{
		Key:   constutil.EmailField,
		Value: req.Email,
	})
	if err != nil {
		s.logger.Error("error", zap.Error(err))
		return
	}
	ems, err := s.Repository.EmsVerify(constutil.ResetPwdEvent, user.Email, req.Captcha)
	if err != nil || ems.ID == 0 {
		return
	}
	newTime := time.Now().Format("2006-01-02 15:04:05")
	oldTime := ems.ExpireAt.Format("2006-01-02 15:04:05")
	if newTime > oldTime {
		err = errors.New(" code verify error")
		return
	}
	err = s.Repository.EmsDelete(ems.ID)
	if err != nil {
		return
	}
	return s.Repository.Pwd(&requests.Pwd{
		NewPwd: req.NewPwd,
		UserID: req.UserID,
	}, user.Salt)
}
